package com.ft.admix.bd.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsRewardVideoAd;
import com.ft.admix.ad.MixCustomRewardVideoAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.model.MixRewardItem;
import com.ft.admix.utils.ThreadUtils;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsVideoPlayConfig;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class RewardAdapterLoader extends MixCustomRewardVideoAdapter {

    KsRewardVideoAd mRewardAd;
    KsLoadManager adRequestManager = KsAdSDK.getLoadManager();

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        if (!KsAdSDK.haseInit()||KsAdSDK.getLoadManager() == null) {
            callLoadFailed(-1, "快手SDK未初始化");
            return;
        }

        KsScene ksScene = new KsScene.Builder(Long.parseLong(config.getAdId()))
                .build();

        adRequestManager.loadRewardVideoAd(ksScene, new KsLoadManager.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                callLoadFailed(i, s);
            }

            @Override
            public void onRewardVideoResult(@Nullable @androidx.annotation.Nullable List<KsRewardVideoAd> list) {
                callLoadSuccess();
            }

            @Override
            public void onRewardVideoAdLoad(@Nullable @androidx.annotation.Nullable List<KsRewardVideoAd> list) {
                if (list != null && list.size() > 0) {
                    mRewardAd = list.get(0);
                    mRewardAd.setRewardAdInteractionListener(new KsRewardVideoAd.RewardAdInteractionListener() {
                        @Override
                        public void onAdClicked() {
                            callRewardVideoAdClick();
                        }

                        @Override
                        public void onPageDismiss() {
                            callRewardVideoAdClose();
                        }

                        @Override
                        public void onVideoPlayError(int i, int i1) {
                            callRewardVideoError();
                        }

                        @Override
                        public void onVideoPlayEnd() {
                            callRewardVideoComplete();
                        }

                        @Override
                        public void onVideoSkipToEnd(long l) {
                            callRewardVideoAdSkipped();
                        }

                        @Override
                        public void onVideoPlayStart() {

                        }

                        @Override
                        public void onRewardVerify() {
                            callRewardVideoRewardVerify(new MixRewardItem() {
                                @Override
                                public boolean rewardVerify() {
                                    return true;
                                }

                                @Override
                                public float getAmount() {
                                    return 0;
                                }

                                @Override
                                public String getRewardName() {
                                    return null;
                                }

                                @Override
                                public Map<String, Object> getCustomData() {
                                    return null;
                                }
                            });
                        }

                        @Override
                        public void onRewardVerify(Map<String, Object> map) {

                        }

                        @Override
                        public void onRewardStepVerify(int i, int i1) {

                        }

                        @Override
                        public void onExtraRewardVerify(int i) {

                        }
                    });
                }
            }
        });
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                if (mRewardAd != null && mRewardAd.isAdEnable()) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }

    @Override
    public void show(Activity activity) {
        if (mRewardAd != null) {
            KsVideoPlayConfig playConfig = new KsVideoPlayConfig.Builder()
                    .videoSoundEnable(true)
                    .build();
            mRewardAd.showRewardVideoAd(activity, playConfig);
        }
    }
} 